/*
 * $Id: AppView.java 134 2009-07-13 10:02:12Z donbeave $
 */

package labour.manager.client.mvc;

import labour.manager.client.AppEvents;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.layout.AccordionLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.user.client.ui.RootPanel;

public class AppView extends View {

  public static final String VIEWPORT = "AppView.VIEWPORT";
  public static final String CENTER_PANEL = "AppView.CENTER_PANEL";
  public static final String EAST_PANEL = "AppView.EAST_PANEL";
  public static final String TAB_PANEL = "AppView.TAB_PANEL";

  private Viewport viewport;
  private LayoutContainer center;
  private ContentPanel east;

  public AppView(Controller controller) {
    super(controller);
  }

  protected void handleEvent(AppEvent event) {
    EventType type = event.getType();

    if (type == AppEvents.Init) {
      onInit();
    }
    else if (type == AppEvents.Login) {
      onLogin();
    }
  }

  protected void onLogin() {
    Dispatcher.forwardEvent(AppEvents.Init);
  }

  protected void onInit() {
    viewport = new Viewport();
    viewport.setLayout(new BorderLayout());

    createCenter();
    createEast();

    TabPanel tabPanel = new TabPanel();
    center.add(tabPanel);

    Registry.register(VIEWPORT, viewport);
    Registry.register(CENTER_PANEL, center);
    Registry.register(EAST_PANEL, east);

    Registry.register(TAB_PANEL, tabPanel);

    RootPanel.get().add(viewport);
  }

  private void createCenter() {
    BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
    data.setMargins(new Margins(5));

    center = new LayoutContainer();
    center.setLayout(new FitLayout());

    viewport.add(center, data);
  }

  private void createEast() {
    BorderLayoutData data = new BorderLayoutData(LayoutRegion.EAST, 300, 150, 350);
    data.setMargins(new Margins(5, 0, 5, 5));

    east = new ContentPanel();
    east.setBodyBorder(false);
    east.setLayoutOnChange(true);
    east.setLayout(new AccordionLayout());

    east.setHeading("Navigator");

    viewport.add(east, data);
  }

}
